﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MDF.DataHelper.BussinessObject.NewsBO;
using WebLibs;

namespace MDF.DataHelper.DataObject.NewsDO
{
    public class CommentDAO : BaseDataObject
    {
        #region Constructor

        public CommentDAO()
        {
        }

        public CommentDAO(IData _objData)
        {
            this.m_objData = _objData;
        }
        #endregion

        #region Methods

        public void Insert(int NewsID, int LangID, string Name, string Email, string Phone, string Address, string _Comment, string IP)
        {
            try
            {
                this.Connect();

                this.m_objData.CreateNewStoredProcedure("[Comment_Insert]");
                this.m_objData.AddParameter("@NewsID", NewsID);
                this.m_objData.AddParameter("@LangID", LangID);
                this.m_objData.AddParameter("@Name", Name);
                this.m_objData.AddParameter("@Email", Email);
                this.m_objData.AddParameter("@Phone", Phone);
                this.m_objData.AddParameter("@Address", Address);
                this.m_objData.AddParameter("@Comment", _Comment);
                this.m_objData.AddParameter("@IP", IP);

                this.m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public DataTable GetNewComments(int iPage, int iPageSize)
        {
            try
            {
                this.Connect();

                this.m_objData.CreateNewStoredProcedure("[Comment_GetNewComments]");
                this.m_objData.AddParameter("@Page", iPage);
                this.m_objData.AddParameter("@PageSize", iPageSize);

                return this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }
        public DataTable Select(int newsID, int iLang)
        {
            try
            {
                this.Connect();

                this.m_objData.CreateNewStoredProcedure("[Comment_GetByNewsID]");
                this.m_objData.AddParameter("@NewsID", newsID);
                this.m_objData.AddParameter("@LangID", iLang);

                return this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }
        public DataTable GetAll(int iPage, int iPageSize)
        {
            try
            {
                this.Connect();

                this.m_objData.CreateNewStoredProcedure("[Comment_GetAll]");
                this.m_objData.AddParameter("@Page", iPage);
                this.m_objData.AddParameter("@PageSize", iPageSize);

                return this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }
        public int CountNotRead()
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[Comment_CountNotRead]");
                var tempTable = this.m_objData.ExecStoreToDataTable();
                if (tempTable == null || tempTable.Rows.Count == 0)
                    return 0;
                return Int32.Parse(tempTable.Rows[0][0].ToString());
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }
        public void Delete(string id)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[Comment_Delete]");
                this.m_objData.AddParameter("@ID", id);
                this.m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public void SetRead(int iCommentID)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[Comment_SetRead]");
                this.m_objData.AddParameter("@ID", iCommentID);
                this.m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        #endregion
    }
}
